package com.example.hadoop.client;

import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.util.Iterator;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 *
 * @Auther: Brian
 * @Date: 2020/04/18/22:20
 * @Description:
 */
public class HadoopStreamDemo {

    FileSystem fs = null;
    Configuration conf = null;
    private static final String USER = "root";

    @Before
    public void init() throws Exception {
        conf = new Configuration();
        fs = FileSystem.get(new URI("hdfs://shizhan:9000"), conf, USER);
    }


    @Test
    public void testUpload() throws Exception {
        FileInputStream inputStream = new FileInputStream("d:/bbb.txt");
        FSDataOutputStream outputStream = fs.create(new Path("/aaa/bbb.txt"), true);
        IOUtils.copy(inputStream, outputStream);
    }

    @Test
    public void testDownload() throws Exception {
        FileOutputStream outputStream = new FileOutputStream("d:/aaa.txt");
        FSDataInputStream inputStream = fs.open(new Path("/aaa/bbb.txt"));
        IOUtils.copy(inputStream, outputStream);
    }

    @Test
    public void testDownloadRandom() throws Exception {
        FileOutputStream outputStream = new FileOutputStream("D:/aaa.txt.part2");
        FSDataInputStream inputStream = fs.open(new Path("/aaa/bbb.txt"));
        inputStream.seek(1000);
        IOUtils.copy(inputStream, outputStream);
    }

    @Test
    public void testCat() throws IOException {
        FSDataInputStream inputStream = fs.open(new Path("/aaa/bbb.txt"));
        IOUtils.copy(inputStream, System.out);
    }

    @After
    public void afterRun() throws IOException {
         fs.close();
    }
}
